var _window;
var _table;
var userersm;
function showWindow(){
	var formSave = new Ext.form.FormPanel({
        labelWidth: 60, 
        bodyStyle:'padding:5px 5px 0',
        height:60,
        defaults: {width: 50},
        loadMask : true,
        layout : 'form',
        items: [{
            	xtype: 'textfield',
            	fieldLabel: '邮件地址',
            	name: 'emailInput',   
            	anchor: '95%',
				maxLength : 50,
				maxLengthText : '长度不能大于50'
            },{
            	xtype: 'button',
            	text: '添加',
            	name: 'name',
            	style:{marginLeft:'155px',marginTop:'5px'},
            	handler: function() {
        			var baseObj = formSave.form.getValues();
            		if(baseObj["emailInput"] == "" || baseObj["emailInput"] == "undefined" || baseObj["emailInput"] == null){
            			Ext.MessageBox.show({ title:Ext.systemTitle,
						   msg: '邮件地址不能为空！',
						   buttons: Ext.Msg.OK,						  					
						   icon: Ext.MessageBox.INFO
						});
            		}else{
        				if(isRepeat(_table,baseObj["emailInput"])){
	            			Ext.Ajax.request({
								url:Ext.get('basePath').getValue()+'tjsinfo/tjsoa/mail/findUserByEmail.do',
							    params: { emailInput: baseObj["emailInput"]},
							    success:function(response){
							    	if(response.responseText=='no'){
								    	Ext.MessageBox.show({ title:Ext.systemTitle,
										   msg: '系统中无此邮箱地址！',
										   buttons: Ext.Msg.OK,							  					
										   icon: Ext.MessageBox.INFO
										});
							    	}else{
								    	Ext.MessageBox.show({ title:Ext.systemTitle,
										   msg: '操作成功！',
										   buttons: Ext.Msg.OK,							  					
										   icon: Ext.MessageBox.INFO
										});
										
										var r = new myRecord({
											userId:response.responseText.split(',')[0],
											userName:response.responseText.split(',')[1],
											emailAddress:baseObj["emailInput"],
											header:'<a href="javaScript:del()">删除</a>'
										});
										
										_table.getStore().add(r);
										if(document.getElementById('uuuuuuu').value!=''){
											document.getElementById('uuuuuuu').value += ','+response.responseText.split(',')[1]+',';
										}else{
											document.getElementById('uuuuuuu').value += response.responseText.split(',')[1]+',';
										}
										document.getElementById('jubaorenInput').value += response.responseText;
										document.getElementById('writeName').value += response.responseText.split(',')[1]+',';
									}
							    },
							    failure:function(){
							    	Ext.MessageBox.show({ title:Ext.systemTitle,
									   msg: '操作失败！',
									   buttons: Ext.Msg.OK,						  					
									   icon: Ext.MessageBox.INFO
									});
							    }
							});	
						}else{
							Ext.MessageBox.show({ title:Ext.systemTitle,
							   msg: '列表中已存在此邮件地址，请重新输入！',
							   buttons: Ext.Msg.OK,						  					
							   icon: Ext.MessageBox.INFO
							});
						}
            		}
            	}
            }
        ],
        region: 'north'
    }); 
	var _columnModle = new Ext.grid.ColumnModel({
 		columns:[
 		{
 			header: "ID", menuDisabled: false,dataIndex:'userId',hidden:true
 		},
 		{
 			header: "姓名", menuDisabled: false,dataIndex:'userName'
 		},{
 			header: "邮箱地址", menuDisabled: false,dataIndex:'emailAddress'
 		},{
 			header: "操作", menuDisabled: false,dataIndex:'header'
 		}]
 	});
	var _store = new Ext.data.SimpleStore({
 		fields:['userId','userName','emailAddress','header']
 	});
 	var myRecord = Ext.data.Record.create([{
		name:'userId',mapping:'userId'
	},{
		name:'userName',mapping:'userName'
	},{
		name:'emailAddress',mapping:'emailAddress'
	},{
		name:'header',mapping:'header'
	}]);
 	
	_table = new Ext.grid.GridPanel({
		colModel:_columnModle,
 		store:_store,
	    width: 500,
	    height:340,
	    title: '列表',
	    renderTo:Ext.get('showRegional'),
	    frame: true,
	    viewConfig:{
	    	forceFit: true
	    },
	    clicksToEdit:1, 
	    layout : 'fit',// 表格内列内容填充满,按列头比例填充 
	    loadMask:{ 
		msg : '数据加载中，请稍后...' 
		}, 
	    stripeRows:true,
        region: 'center'
	});
		
	_window = new Ext.Window({
        title: '手动输入邮件',
        width: 400,
        height:400,
        layout: 'fit',
        plain:true,
        bodyStyle:'padding:5px;',
        buttonAlign:'center',
        items:[{  
            baseCls:"x-plain",  
            layout:"border",
            items:[formSave,_table]  
        }],
        closable : false,
        buttons: [
	        {
            text: '关闭',
            handler: function() {
            	formSave.form.findField('emailInput').setValue('');
            	_window.hide();
        	}}
	    ]
    });
    _window.show();
}

var bl = true;
//function showPeople(type) {  
	//peopleTree();
	/**Ext.MessageBox.show({
        title: '提示框',
        msg:'点击下列按钮,选择录入类型',
        width: 300,
        buttons: { ok: "菜单树选择", no:"手动输入",cancel:'关闭' },  
        fn: function(btn, text) {
       		if (btn == 'ok') {
       			//selectusercheckwinshow('jubaorenid','uuuuuuu','node');
       			peopleTree();
            }else if(btn == 'no'){
            	if(bl){
            		showWindow();
            		bl = false;
            	}else{
            		_window.show();
            	}
            }
        }
    });**/
//}

function isRepeat(grid,value){
	var store = grid.store;
    var rowCount = store.getCount(); //记录数
    var cm = grid.getColumnModel();
    var colCount = cm.getColumnCount(); //列数
    var view = grid.getView();
    var str_string = "";
    for (i = 0; i < rowCount; i++) {
        var rs = view.getCell(i, 1).innerText;
        if (rs == "" || rs == "undefined" || rs == null) {
        } else {
            for (j = 1; j < colCount; j++) {
                var cell = view.getCell(i, j);
                var text = cell.innerText;
                if(text==trim(value)){
                	return false;
                }
            }
        }
    }
    return true;
}

function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g,"");
}

function del(){
	var selectedRow = _table.getSelectionModel().getSelected();
	
	var val = document.getElementById('uuuuuuu').value;
	var valwrite = document.getElementById('writeName').value;
	var valid = document.getElementById('jubaorenInput').value;

	if(val.indexOf(selectedRow.get("userName"))!=-1){
		document.getElementById('uuuuuuu').value = val.replace(selectedRow.get("userName")+',','');
	}
	if(valwrite.indexOf(selectedRow.get("userName"))!=-1){
		document.getElementById('writeName').value = valwrite.replace(selectedRow.get("userName")+',','');
	}
	if(valid.indexOf(','+selectedRow.get("userId")+',')!=-1){
		document.getElementById('jubaorenInput').value = valid.replace(selectedRow.get("userId")+',','');
	}
	var ds = _table.getStore(); 
	if (selectedRow){
		ds.remove(selectedRow);
		Ext.MessageBox.show({ title:Ext.systemTitle,
		   msg: '操作成功！',
		   buttons: Ext.Msg.OK,	
		   icon: Ext.MessageBox.INFO
		});
	}
	if(document.getElementById('jubaorenInput').value==','){
		document.getElementById('jubaorenInput').value = '';
	}
}
